import pandas as pd
from config import config
from utils.read_url import get_url


def parse_saving(html_soup):
    arr = []

    soup = html_soup.find('tbody')
    if soup:
        info_list = soup.find_all('tr')
        if info_list:
            for num, info in enumerate(info_list):
                num_data = info.find_all('td')
                if num_data:
                    num_data = num_data[1].get_text()
                    if num in [2, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 18, 19]:
                        arr.append(num_data)
    return arr


def read_saving():
    arr = []
    html_soup = get_url(url=config.RMB_SAVING_URL)
    if html_soup:
        url_soup = html_soup.find_all('ul')[-1]
        deposit_url = url_soup.find_all('li')[0].find('a').get('href')
        deposit_url = '{}{}'.format(config.RMB_SAVING_URL, deposit_url[deposit_url.index('/') + 1:])


        html_info_soup = get_url(url=deposit_url)
        if html_info_soup:
            arr = parse_saving(html_info_soup)

    df = pd.DataFrame(arr).T
    if not df.empty:
        df.columns = ['demand', 'time_whole_three_months', 'time_whole_six_months', 'time_whole_one_year',
                      'time_whole_two_years', 'time_whole_three_years', 'time_whole_five_years',
                      'time_no_whole_one_year', 'time_no_whole_three_years', 'time_no_whole_five_years', 'flexible',
                      'agreement', 'notice_day', 'notice_seven_days']
    return df


df = read_saving()
print(df)
